program trade_around_annc
      character*6 ticker(5500)
      character*8 cusp(5500),cusip(450000),lcusip,fcusip(50000)
      integer(kind=8):: nshrs,nvalue,jvalue,ivol(5500),numshrs(5500),ntot(5500),navail(5500)
      dimension nep(5500),ntype(5500),jyr(5500),jmn(5500),jdy(5500),retrn(5500),pctonloan(5500),ishrout(5500),vwret(5500),util(5500) 
      dimension jast(5500),itickt(5500),age(5500),favg(5500),fmax(5500),fmin(5500),fstd(5500),iperm(5500),price(5500),ncbeg(50000),iadum(5500)
      dimension jdate(5500),jhr(5500),turn(21),aret(21)
!      open(unit=10,file='e:\FIS_duration\episode_data_total_anndate.txt')
!      open(unit=99,file='e:\FIS_duration\ret_turn_by_anndate.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_total_anndate_rep.txt')
      open(unit=99,file='e:\FIS_duration\ret_turn_by_anndate_rep.txt')
      
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),ntype(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),navail(i),ntot(i),iadum(i),jhr(i)
1000  format(1x,i4,1x,i1,1x,i8,1x,i8,1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,i12,1x,i1,1x,i2)
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      do 20 ii=1,icnt
          if(iadum(ii).eq.0) go to 20
          turn=-9.9
          aret=-9.9
          k=ii
          if(jhr(ii).ge.16) k=ii+1
          nn=0
          kbg=k-10
          kend=k+10
          do 10 kk=kbg,kend
              nn=nn+1
              if(kk.lt.1.or.kk.gt.icnt) go to 10
              turn(nn)=float(ivol(kk))/(1000.*float(ishrout(kk)))
              aret(nn)=retrn(kk)-vwret(kk)
10        continue
          write(99,9900)nep(1),ntype(1),jdate(ii),(turn(nn),nn=1,21),(aret(nn),nn=1,21)
9900      format(1x,i4,1x,i1,1x,i8,21(1x,f8.5),21(1x,f9.6))
20    continue          
          
100   continue
      nep(1)=nep(i)
      ntype(1)=ntype(i)
      jyr(1)=jyr(i)
      jmn(1)=jmn(i)
      jdy(1)=jdy(i)
      retrn(1)=retrn(i)
      vwret(1)=vwret(i)
      ntot(1)=ntot(i)
      numshrs(1)=numshrs(i)
      navail(1)=navail(i)
      jast(1)=jast(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      ishrout(1)=ishrout(i)
      util(1)=util(i)
      ntot(1)=ntot(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program trade_around_annc